package com.example.othree.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.othree.common.PageParam;
import com.example.othree.common.R;
import com.example.othree.entity.Drone;
import com.example.othree.entity.TowerOzoneGradientObservations;
import com.example.othree.service.TowerOzoneManagementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 铁塔数据管理Controller
 * Created by zr on 2024/10/17.
 */
@RestController
@CrossOrigin
@RequestMapping("/api")
public class TowerOzoneManagementController {
    @Autowired
    private TowerOzoneManagementService towerOzoneManagementService;

    @PostMapping("/tower/create")
    @ApiOperation("新增")
    public R create(@RequestBody TowerOzoneGradientObservations towerOzoneGradientObservations){
        return towerOzoneManagementService.save(towerOzoneGradientObservations) ? R.ok() : R.error();
    }

    @PostMapping("/tower/saveBatch")
    @ApiOperation("批量新增")
    public R saveBatch(@RequestBody List<TowerOzoneGradientObservations> towerOzoneGradientObservations){
        return towerOzoneManagementService.saveBatch(towerOzoneGradientObservations) ? R.ok() : R.error();
    }

    @GetMapping("/tower/list")
    public List<TowerOzoneGradientObservations> list(){
        return towerOzoneManagementService.list();
    }

    @PostMapping("/tower/update")
    @ApiOperation("修改")
    public R update(@RequestBody TowerOzoneGradientObservations towerOzoneGradientObservations){
        return towerOzoneManagementService.updateById(towerOzoneGradientObservations) ? R.ok() : R.error();
    }

    @PostMapping("/tower/delete/{id}")
    public R delete(@PathVariable Integer id){
        return towerOzoneManagementService.removeById(id) ? R.ok() : R.error();
    }

    @PostMapping("/tower/listPage")
    @ApiOperation("查询、分页")
    public R listPage(@RequestBody PageParam pageParam){
        HashMap param = pageParam.getParam();
        String PointName = param.get("PointName").toString();
        String StationCode = param.get("StationCode").toString();
        String time1 = param.get("earliestTime").toString();
        String time2 = param.get("latestTime").toString();

        Page<TowerOzoneGradientObservations> page = new Page();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<TowerOzoneGradientObservations> lambdaQueryWrapper = new LambdaQueryWrapper();

        if(StringUtils.isNotBlank(PointName) && !"null".equals(PointName)){
            lambdaQueryWrapper.eq(TowerOzoneGradientObservations::getPointName, PointName);
        }
        if(StringUtils.isNotBlank(StationCode) && !"null".equals(StationCode)){
            lambdaQueryWrapper.eq(TowerOzoneGradientObservations::getStationCode, StationCode);
        }
        if(StringUtils.isNotBlank(time1) && StringUtils.isNotBlank(time1) && !"null".equals(time1) && !"null".equals(time2)){
            if(time1.compareTo(time2) == 0){
                lambdaQueryWrapper.eq(TowerOzoneGradientObservations::getTime,time1);
            } else if (time1.compareTo(time2)>0) {
                return R.error().message("时间区间选择错误！");
            }else {
                lambdaQueryWrapper.ge(TowerOzoneGradientObservations::getTime, time1);
                lambdaQueryWrapper.le(TowerOzoneGradientObservations::getTime, time2);
            }
        }

        IPage result = towerOzoneManagementService.PageList(page, lambdaQueryWrapper);

        return R.ok().data("result", result);
    }
}
